package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.irremote.lib.net.IRextReqManager;
import com.stark.irremote.lib.net.bean.IrCity;
import com.stark.irremote.lib.net.bean.IrProvince;
import com.stark.irremote.lib.net.bean.IrStbOperator;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelCityAdapter;
import flc.ast.adapter.SelOperatorAdapter;
import flc.ast.adapter.SelProvincesAdapter;
import flc.ast.databinding.ActivitySelAddressBinding;
import stark.common.basic.retrofit.IReqRetCallback;

public class SelAddressActivity extends BaseAc<ActivitySelAddressBinding> {

    private SelProvincesAdapter provincesAdapter;
    private SelCityAdapter cityAdapter;
    private SelOperatorAdapter operatorAdapter;
    private String code;//选择的城市编号
    private int position = 0;//返回时判断当前选择的位置


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_address;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelAddressBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //省份
        mDataBinding.rvSelProvinceList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        provincesAdapter = new SelProvincesAdapter();
        mDataBinding.rvSelProvinceList.setAdapter(provincesAdapter);
        provincesAdapter.setOnItemClickListener(this);
        //城市
        mDataBinding.rvSelCityList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        cityAdapter = new SelCityAdapter();
        mDataBinding.rvSelCityList.setAdapter(cityAdapter);
        cityAdapter.setOnItemClickListener(this);
        //运营商
        mDataBinding.rvSelOperatorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        operatorAdapter = new SelOperatorAdapter();
        mDataBinding.rvSelOperatorList.setAdapter(operatorAdapter);
        operatorAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == provincesAdapter) {
            code = provincesAdapter.getItem(position).code;
            getCity();
        } else if (adapter == cityAdapter) {
            getOperator(cityAdapter.getItem(position).code);
        } else if (adapter == operatorAdapter) {
            RemoteJdhActivity.isAdd = true;
            RemoteJdhActivity.bean = operatorAdapter.getItem(position);
            startActivity(RemoteJdhActivity.class);
        }
    }

    @Override
    protected void initData() {
        //获取省份
        getProvinces();
    }

    @Override
    public void onBackPressed() {
        if (position == 0) {
            super.onBackPressed();
        } else if (position == 1) {
            position = 0;
            mDataBinding.tvSelAddressTitle.setText(R.string.sel_provinces);
            mDataBinding.rvSelProvinceList.setVisibility(View.VISIBLE);
            mDataBinding.rvSelCityList.setVisibility(View.GONE);
            mDataBinding.rvSelOperatorList.setVisibility(View.GONE);
        } else {
            position = 1;
            mDataBinding.tvSelAddressTitle.setText(R.string.sel_city);
            mDataBinding.rvSelProvinceList.setVisibility(View.GONE);
            mDataBinding.rvSelCityList.setVisibility(View.VISIBLE);
            mDataBinding.rvSelOperatorList.setVisibility(View.GONE);
        }
    }

    //获取省份
    private void getProvinces() {
        position = 0;
        mDataBinding.tvSelAddressTitle.setText(R.string.sel_provinces);
        mDataBinding.rvSelProvinceList.setVisibility(View.VISIBLE);
        mDataBinding.rvSelCityList.setVisibility(View.GONE);
        mDataBinding.rvSelOperatorList.setVisibility(View.GONE);
        showDialog(getString(R.string.loading));
        IRextReqManager.irextApi().listProvinces(this, new IReqRetCallback<List<IrProvince>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<IrProvince> result) {
                dismissDialog();
                if (result == null) {
                    ToastUtils.showShort(msg);
                    return;
                }
                provincesAdapter.setList(result);
            }
        });
    }

    //获取省份
    private void getCity() {
        position = 1;
        mDataBinding.tvSelAddressTitle.setText(R.string.sel_city);
        mDataBinding.rvSelProvinceList.setVisibility(View.GONE);
        mDataBinding.rvSelCityList.setVisibility(View.VISIBLE);
        mDataBinding.rvSelOperatorList.setVisibility(View.GONE);
        if (code != null && code.length() > 2) {
            code = code.substring(0, 2);
        }
        showDialog(getString(R.string.loading));
        IRextReqManager.irextApi().listCities(this, code, new IReqRetCallback<List<IrCity>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<IrCity> result) {
                dismissDialog();
                if (result == null) {
                    ToastUtils.showShort(msg);
                    return;
                }
                cityAdapter.setList(result);
            }
        });
    }

    //获取运营商
    private void getOperator(String code) {
        position = 2;
        mDataBinding.tvSelAddressTitle.setText(R.string.sel_operator);
        mDataBinding.rvSelProvinceList.setVisibility(View.GONE);
        mDataBinding.rvSelCityList.setVisibility(View.GONE);
        mDataBinding.rvSelOperatorList.setVisibility(View.VISIBLE);
        showDialog(getString(R.string.loading));
        IRextReqManager.irextApi().listOperators(this, code, new IReqRetCallback<List<IrStbOperator>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<IrStbOperator> result) {
                dismissDialog();
                if (result == null) {
                    ToastUtils.showShort(msg);
                    return;
                }
                operatorAdapter.setList(result);
            }
        });
    }

}